﻿using B.S.BasicData.Api.Read.Application.Command.User;
using B.S.BasicData.Api.Read.DTO;
using B.S.BasicData.Common;
using B.S.BasicData.Domian.Entities.Account;
using B.S.BasicData.Domian.Entities.RBAC;
using B.S.BasicData.Domian.Entities.shenpi;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MD5Hash;
using MediatR;
using Yitter.IdGenerator;

namespace B.S.BasicData.Api.Read.Application.Handler.User
{
    public class LoginHandler : IRequestHandler<LoginCommand, ApiResult<LoginDTO>>
    {
        private readonly IBaseRepository<AccountModel> accountRepo;
        private readonly IBaseRepository<NlogModel> nlogRepo;
        private readonly IBaseRepository<RoleModel> rolegRepo;
        private readonly IBaseRepository<UserRoleModel> userroleRepo;
        private readonly IBaseRepository<LeaveModel> leaveRepo;
        private readonly JwtTools jwtTools;
        private readonly IConfiguration _configuration;

        public LoginHandler(IBaseRepository<AccountModel> accountRepo, IBaseRepository<NlogModel> nlogRepo, IBaseRepository<RoleModel> rolegRepo, IBaseRepository<UserRoleModel> userroleRepo,IBaseRepository<LeaveModel>leaveRepo,JwtTools jwtTools,IConfiguration _configuration)
        {
            this.accountRepo = accountRepo;
            this.nlogRepo = nlogRepo;
            this.rolegRepo = rolegRepo;
            this.userroleRepo = userroleRepo;
            this.leaveRepo = leaveRepo;
            this.jwtTools = jwtTools;
            this._configuration = _configuration;
        }
        public Task<ApiResult<LoginDTO>> Handle(LoginCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<LoginDTO>();
            var account = accountRepo.GetAll().Where(x => x.Name == request.Name).FirstOrDefault();
            if (account == null)
            {
                res.Message = "账号不存在";
                res.Code = ApiEnum.Error;
                return Task.FromResult(res);
            }
            //var Pwd = request.Pwd.GetMD5();
            if (account.Pwd !=request.Pwd)
            {
                res.Message = "登录失败 密码有误";
                res.Code = ApiEnum.Error;
                return Task.FromResult(res);
            }
            var userid=account.Id;
            var userrole=userroleRepo.GetAll().Where(x=>x.UserId ==userid).FirstOrDefault();
            var role=rolegRepo.GetValue(userrole.RoleId).Result;

           // var leave =leaveRepo.GetValue(userrole.RoleId).Result;
            LoginDTO loginDTO = new LoginDTO()
            {
                Id = userid,
                Name = request.Name,
                Pwd = request.Pwd,
                RoleId=role.RoleId,
                RoleName= role.RoleName,
                bid = account.bid,
                token= jwtTools.JwtNewString(_configuration, account.Id.ToString())//在dto里面定义token 然后在这传数据 OK

        };
            NlogModel nlogModel = new NlogModel()
            {
                Id = YitIdHelper.NextId(),
                NlogContext = $"用户{account.Name}登录了",
                CreatePeople = account.Name,
                CreateTime = DateTime.Now,
            };
           int n=nlogRepo.Add(nlogModel).Result;
            res.Message = "登录成功";
            res.Code = ApiEnum.Ok;
            res.Data = loginDTO;
            return Task.FromResult(res);
        }
    }
}
